#include "../include/user_page.h"

static lv_ft_info_t info;
static lv_style_t style;

static lv_obj_t *outer_page;
static lv_obj_t *detail_page;
static lv_obj_t *pay_page;
static lv_obj_t *temp_page = NULL;

static lv_obj_t *lb_buy_num;
static int buy_num = 1;
static int max_buy_num;

static lv_obj_t *lb_total_price;
static long unit_price;
static long total_price;

extern void show_admin_login_page();

void user_page_init()
{
    info.name = "/font/simkai.ttf";
    info.weight = 20;
    info.style = FT_FONT_STYLE_NORMAL;
    info.mem = NULL;
    if(!lv_ft_font_init(&info)) {
        LV_LOG_ERROR("create failed.");
    }

    lv_style_init(&style);
    lv_style_set_text_font(&style, info.font);

    show_outer_page();
}

void show_outer_page()
{
    outer_page = lv_obj_create(NULL);
    lv_obj_set_size(outer_page, 800, 480);
    lv_obj_set_style_pad_all(outer_page, 0, 0);

    lv_obj_t *lb_title = lv_label_create(outer_page);
    lv_obj_align(lb_title, LV_ALIGN_TOP_MID, 0, 40);
    lv_obj_add_style(lb_title, &style, 0);
    lv_label_set_text(lb_title, "欢迎使用盲盒售卖机");

    lv_obj_t *btn_admin_login = lv_btn_create(outer_page);
    lv_obj_align(btn_admin_login, LV_ALIGN_TOP_RIGHT, 0, 25);
    lv_obj_set_size(btn_admin_login, 150, 50);
    lv_obj_t *lb_admin_login = lv_label_create(btn_admin_login);
    lv_obj_add_style(lb_admin_login, &style, 0);
    lv_obj_center(lb_admin_login);
    lv_label_set_text(lb_admin_login, "管理员登录");
    lv_obj_add_event_cb(btn_admin_login, show_admin_login_page_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn1 = lv_btn_create(outer_page);
    lv_obj_set_pos(btn1, 55, 100);
    lv_obj_set_size(btn1, 150, 250);
    lv_obj_t *img1 = lv_img_create(btn1);
    lv_img_set_src(img1, get_curr_path());
    lv_obj_align(img1, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_t *lb1 = lv_label_create(btn1);
    lv_obj_align(lb1, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_add_style(lb1, &style, 0);
    lv_label_set_text(lb1, get_curr_name());
    lv_obj_add_event_cb(btn1, show_detail_cb, LV_EVENT_CLICKED, NULL);

    set_curr_next();

    lv_obj_t *btn2 = lv_btn_create(outer_page);
    lv_obj_align_to(btn2, btn1, LV_ALIGN_OUT_RIGHT_TOP, 30, 0);
    lv_obj_set_size(btn2, 150, 250);
    lv_obj_t *img2 = lv_img_create(btn2);
    lv_img_set_src(img2, get_curr_path());
    lv_obj_align(img2, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_t *lb2 = lv_label_create(btn2);
    lv_obj_align(lb2, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_add_style(lb2, &style, 0);
    lv_label_set_text(lb2, get_curr_name());
    lv_obj_add_event_cb(btn2, show_detail_cb, LV_EVENT_CLICKED, NULL);

    set_curr_next();

    lv_obj_t *btn3 = lv_btn_create(outer_page);
    lv_obj_align_to(btn3, btn2, LV_ALIGN_OUT_RIGHT_TOP, 30, 0);
    lv_obj_set_size(btn3, 150, 250);
    lv_obj_t *img3 = lv_img_create(btn3);
    lv_img_set_src(img3, get_curr_path());
    lv_obj_align(img3, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_t *lb3 = lv_label_create(btn3);
    lv_obj_align(lb3, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_add_style(lb3, &style, 0);
    lv_label_set_text(lb3, get_curr_name());
    lv_obj_add_event_cb(btn3, show_detail_cb, LV_EVENT_CLICKED, NULL);

    set_curr_next();

    lv_obj_t *btn4 = lv_btn_create(outer_page);
    lv_obj_align_to(btn4, btn3, LV_ALIGN_OUT_RIGHT_TOP, 30, 0);
    lv_obj_set_size(btn4, 150, 250);
    lv_obj_t *img4 = lv_img_create(btn4);
    lv_img_set_src(img4, get_curr_path());
    lv_obj_align(img4, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_t *lb4 = lv_label_create(btn4);
    lv_obj_align(lb4, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_add_style(lb4, &style, 0);
    lv_label_set_text(lb4, get_curr_name());
    lv_obj_add_event_cb(btn4, show_detail_cb, LV_EVENT_CLICKED, NULL);

    // 底部标签
    lv_obj_t *lb_tip = lv_label_create(outer_page);
    lv_obj_align(lb_tip, LV_ALIGN_BOTTOM_MID, 0, -50);
    lv_obj_add_style(lb_tip, &style, 0);
    lv_label_set_text(lb_tip, "请点击要购买的盲盒");

    lv_obj_t *btn_prev = lv_btn_create(outer_page);
    lv_obj_align(btn_prev, LV_ALIGN_BOTTOM_LEFT, 0, -40);
    lv_obj_set_size(btn_prev, 100, 50);
    lv_obj_t *lb_prev = lv_label_create(btn_prev);
    lv_obj_add_style(lb_prev, &style, 0);
    lv_label_set_text(lb_prev, "上一页");
    lv_obj_add_event_cb(btn_prev, prev_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_next = lv_btn_create(outer_page);
    lv_obj_align(btn_next, LV_ALIGN_BOTTOM_RIGHT, 0, -40);
    lv_obj_set_size(btn_next, 100, 50);
    lv_obj_t *lb_next = lv_label_create(btn_next);
    lv_obj_add_style(lb_next, &style, 0);
    lv_label_set_text(lb_next, "下一页");
    lv_obj_add_event_cb(btn_next, next_cb, LV_EVENT_CLICKED, NULL);

    lv_scr_load(outer_page);

    if(temp_page)
    {
        lv_obj_del(temp_page);
        temp_page = NULL;
    }
}

void show_admin_login_page_cb(lv_event_t *e)
{
    curr_init();
    show_admin_login_page();
    lv_obj_del(outer_page);
}

void show_detail_cb(lv_event_t *e)
{
    lv_obj_t *btn_curr_clicked = lv_event_get_current_target(e);
    Node *curr_clicked_node = NULL;
    uint32_t count = lv_obj_get_child_cnt(btn_curr_clicked);  // 子对象数量
    for(uint32_t i = 0; i < count; i++) 
    {
        lv_obj_t *child = lv_obj_get_child(btn_curr_clicked, i);
        if(lv_obj_check_type(child, &lv_label_class)) 
        {
            char *str = lv_label_get_text(child);
            curr_clicked_node = find_node(str);
            if(curr_clicked_node == NULL)
            {
                printf("没有找到对应的商品\n");
                exit(1);
            }
            break;
        }
    }

    show_detail_page(curr_clicked_node);
}

void prev_cb(lv_event_t *e)
{
    temp_page = outer_page;
    for(int i = 0; i < 7; i++)
    {
        set_curr_prev();
    }
    show_outer_page();
}

void next_cb(lv_event_t *e)
{
    temp_page = outer_page;
    set_curr_next();
    show_outer_page();
}

void show_detail_page(Node *curr_clicked_node)
{   
    detail_page = lv_obj_create(NULL);
    lv_obj_set_size(detail_page, 800, 480);
    lv_obj_set_style_pad_all(detail_page, 0, 0);

    lv_obj_t *btn_ret = lv_btn_create(detail_page);
    lv_obj_set_size(btn_ret, 100, 50);
    lv_label_t *lb_ret = lv_label_create(btn_ret);
    lv_obj_add_style(lb_ret, &style, 0);
    lv_obj_center(lb_ret);
    lv_label_set_text(lb_ret, "返回");
    lv_obj_add_event_cb(btn_ret, detail_ret_outer_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *img = lv_img_create(detail_page);
    lv_img_set_src(img, curr_clicked_node->image_path);
    lv_obj_set_pos(img, 230, 60);
    
    lv_obj_t *lb_name = lv_label_create(detail_page);
    lv_obj_align_to(lb_name, img, LV_ALIGN_OUT_RIGHT_TOP, 50, 20);
    char name[100] = "名称：";
    strncat(name, curr_clicked_node->name, sizeof(name));
    lv_obj_add_style(lb_name, &style, 0);
    lv_label_set_text(lb_name, name);

    lv_obj_t *lb_quantity = lv_label_create(detail_page);
    lv_obj_align_to(lb_quantity, lb_name, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    char quantity[100] = "剩余数量：";
    char temp_num[50] = {0};
    snprintf(temp_num, sizeof(temp_num), "%ld", curr_clicked_node->quantity);
    strncat(quantity, temp_num, sizeof(quantity));
    lv_obj_add_style(lb_quantity, &style, 0);
    lv_label_set_text(lb_quantity, quantity);

    lv_obj_t *lb_price = lv_label_create(detail_page);
    lv_obj_align_to(lb_price, lb_quantity, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    char price[100] = "价格：";
    memset(temp_num, 0, sizeof(temp_num));
    snprintf(temp_num, sizeof(temp_num), "%ld", curr_clicked_node->price);
    strncat(price, temp_num, sizeof(price));
    strncat(price, "元/个", sizeof(price));
    lv_obj_add_style(lb_price, &style, 0);
    lv_label_set_text(lb_price, price);

    max_buy_num = curr_clicked_node->quantity;
    unit_price = curr_clicked_node->price;

    lv_obj_t *lb_buy_num_title = lv_label_create(detail_page);
    lv_obj_align_to(lb_buy_num_title, img, LV_ALIGN_OUT_BOTTOM_LEFT, 30, 50);
    lv_obj_add_style(lb_buy_num_title, &style, 0);
    lv_label_set_text(lb_buy_num_title, "购买数：");

    lb_buy_num = lv_label_create(detail_page);
    lv_obj_align_to(lb_buy_num, lb_buy_num_title, LV_ALIGN_OUT_RIGHT_TOP, 5, 0);
    lv_obj_add_style(lb_buy_num, &style, 0);
    lv_label_set_text(lb_buy_num, "1");

    lv_obj_t *lb_total_price_title = lv_label_create(detail_page);
    lv_obj_align_to(lb_total_price_title, lb_buy_num, LV_ALIGN_OUT_RIGHT_TOP, 50, 0);
    lv_obj_add_style(lb_total_price_title, &style, 0);
    lv_label_set_text(lb_total_price_title, "总价：");

    lb_total_price = lv_label_create(detail_page);
    lv_obj_align_to(lb_total_price, lb_total_price_title, LV_ALIGN_OUT_RIGHT_TOP, 5, 0);
    lv_obj_add_style(lb_total_price, &style, 0);
    char buf[50] = {0};
    snprintf(buf, sizeof(buf), "%ld", unit_price);
    strncat(buf, "元", sizeof(buf));
    lv_label_set_text(lb_total_price, buf);

    lv_obj_t *btn_minus = lv_btn_create(detail_page);
    lv_obj_align(btn_minus, LV_ALIGN_BOTTOM_MID, -50, -20);
    lv_obj_set_size(btn_minus, 50, 50);
    lv_obj_t *lb_minus = lv_label_create(btn_minus);
    lv_obj_set_style_text_font(lb_minus, &lv_font_montserrat_20, 0);
    lv_obj_center(lb_minus);
    lv_label_set_text(lb_minus, "-");
    lv_obj_add_event_cb(btn_minus, plus_minus_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_plus = lv_btn_create(detail_page);
    lv_obj_align_to(btn_plus, btn_minus, LV_ALIGN_OUT_RIGHT_TOP, 20, 0);
    lv_obj_set_size(btn_plus, 50, 50);
    lv_obj_t *lb_plus = lv_label_create(btn_plus);
    lv_obj_set_style_text_font(lb_plus, &lv_font_montserrat_20, 0);
    lv_obj_center(lb_plus);
    lv_label_set_text(lb_plus, "+");
    lv_obj_add_event_cb(btn_plus, plus_minus_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *btn_buy = lv_btn_create(detail_page);
    lv_obj_set_size(btn_buy, 100, 50);
    lv_obj_align(btn_buy, LV_ALIGN_BOTTOM_RIGHT, 0, -20);
    lv_obj_t *lb_buy = lv_label_create(btn_buy);
    lv_obj_add_style(lb_buy, &style, 0);
    lv_obj_center(lb_buy);
    lv_label_set_text(lb_buy, "购买");
    lv_obj_add_event_cb(btn_buy, buy_cb, LV_EVENT_CLICKED, NULL);

    lv_scr_load(detail_page);
}

void detail_ret_outer_cb(lv_event_t *e)
{
    buy_num = 1;
    lv_scr_load(outer_page);
    lv_obj_del(detail_page);
}

void plus_minus_cb(lv_event_t * e)
{
    lv_obj_t *btn = lv_event_get_target(e);
    const char *txt = lv_label_get_text(lv_obj_get_child(btn, 0));

    if(strcmp(txt, "+") == 0 && buy_num < max_buy_num) 
    {
        buy_num++;
    }
    else if(strcmp(txt, "-") == 0 && buy_num > 1) 
    {
        buy_num--;
    }

    total_price = unit_price * buy_num; 

    char buf[50] = {0};
    snprintf(buf, sizeof(buf), "%d", buy_num);
    lv_label_set_text(lb_buy_num, buf);
    
    memset(buf, 0, sizeof(buf));
    snprintf(buf, sizeof(buf), "%ld", total_price);
    strncat(buf, "元", sizeof(buf));
    lv_label_set_text(lb_total_price, buf);
}

void buy_cb(lv_event_t *e)
{
    show_pay_page();
}

void show_pay_page()
{
    pay_page = lv_obj_create(NULL);
    lv_obj_set_size(pay_page, 800, 480);

    lv_obj_t *btn_ret = lv_btn_create(pay_page);
    lv_obj_set_size(btn_ret, 100, 50);
    lv_obj_t *lb_ret = lv_label_create(btn_ret);
    lv_obj_add_style(lb_ret, &style, 0);
    lv_obj_center(lb_ret);
    lv_label_set_text(lb_ret, "返回");
    lv_obj_add_event_cb(btn_ret, pay_ret_detail_cb, LV_EVENT_CLICKED, NULL);

    char *pay_info[100] = {0};
    snprintf(pay_info, sizeof(pay_info), "请支付：%ld元", total_price);
    lv_obj_t *lb_pay_info = lv_label_create(pay_page);
    lv_obj_align(lb_pay_info, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_add_style(lb_pay_info, &style, 0);
    lv_label_set_text(lb_pay_info, pay_info);

    lv_obj_t *img = lv_img_create(pay_page);
    lv_img_set_src(img, "S:/project1/images/pay.jpg");
    lv_obj_align(img, LV_ALIGN_BOTTOM_MID, 0, -50);

    lv_scr_load(pay_page);
}

void pay_ret_detail_cb(lv_event_t *e)
{
    lv_scr_load(detail_page);
    lv_obj_del(pay_page);
}